<!DOCTYPE html>
<html>
<head>
<title>职位管理</title>

<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="职位管理页面">
<meta name="content-type" content="text/html; charset=UTF-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

<link rel="stylesheet" href="/Hospital/lib/layui-v2.1.6/layui/css/layui.css" media="all">
<link rel="stylesheet" href="/Hospital/resources/css/table.css" media="all">
</head>

<body>
	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
		<legend>职位管理</legend>
		<button id="btn_add" class="layui-btn btn_right">
			<i class="layui-icon">&#xe61f;</i> 添加
		</button>
	</fieldset>
	<table id="positionTable" class="layui-table table" lay-filter="positionTable"></table>
</body>
<script src="/Hospital/lib/jquery.min.js" charset="utf-8"></script>
<script src="/Hospital/lib/layui-v2.1.6/layui/layui.js" charset="utf-8"></script>
<script id="operator" type="text/html">
	<a class="layui-btn layui-btn-mini" lay-event="edit">编辑</a>
</script>
<script type="text/javascript">
	layui.use([ "table", "layer", "element" ], function() {
		var $ = layui.jquery;
		var table = layui.table;
		var element = layui.element;
		var layer = layui.layer;
		
		initTable(); //初始化表格;
		btnAddEvent();//新增按钮 监听事件
		//初始化表格
		function initTable() {
			var height = window.innerHeight*0.9;
			var loginUser = JSON.parse(localStorage.getItem("loginUser"));
			
			var permission = JSON.parse(localStorage.getItem("loginUser")).permission;
			//添加职位按钮
			if(loginUser.permission.createProject == 5) {
				$("#btn_add").css({"display":"block"});
			}else {
				$("#btn_add").css({"display":"none"});
			}
			
			//表格初始化
			table.render({
				id : "positionTable",
				elem : "#positionTable",
				page : true,
				limit : 10,
				center : true,
				cols : createCols(),
				url : "/Hospital/permissionController/getAllPosition",
				method : "post",
				where : "",
				response : {
					statusName : "code",
					statusCode : "200",
					countName : "total",
					msgName : "msg",
					dataName : "result",
				}
			});
			
			//监听工具条  lay-filter
			table.on('tool(positionTable)', function(obj){
				//将当行数据写入local
				var data = obj.data;
				//console.log("positionTable.html/obj.data");
				//console.log(data);
				localStorage.setItem("positionDetail", JSON.stringify(data));
				
				//编辑按钮
				if(obj.event === "edit") {
					editClick(data);
				}
			});
		}
		
		//编辑按钮点击事件
		function editClick(data) {
			//编辑按钮点击事件
			//positionTable_operator: 1(新增);2(编辑);
			localStorage.setItem("positionTable_operator", 2);
			layer.open({
				type : 2,
				title : "职位编辑窗口",
				area : ["80%","80%"],
				content : '/Hospital/views/positionManager/positionDetail.html',
				btn : ["确认","取消"],
				btnAlign : "r",
				resize : true,
				yes : function(index, layero) {
					var param = $(layero).find("iframe")[0].contentWindow.getParam();
					var mainFrame = index;
					//console.log("positionTable.html/param");
					//console.log(param);
					//console.log("positionTable.html/obj.data");
					//console.log(data);
					 //数据验证
					if(!vertify(param)) {
						return;
					}
					 
					//如果没有修改直接显示修改成功
					if(isAlter(param,data)) {
					 	layer.close(mainFrame);
					 	localStorage.removeItem("positionDetail");
					 	return;
					}
					 
					//插入数据库
					insertToDB(param,mainFrame);
				},
				cancel : function(index, layero) {
					//左上角X关闭按钮
					localStorage.removeItem("positionDetail");
					layer.close(index);
				},
				btn2 : function(index, layero) {
					//取消按钮
					localStorage.removeItem("positionDetail");
					layer.close(index);
				}
			});
		}
		
		//插入数据库
		function insertToDB(param,mainFrame) {
			console.log("positionTable.insertToDB param");
			console.log(param);
			var operator = localStorage.getItem("positionTable_operator");
			var text = "";
			var url = "";
			if(operator == 1) {
				//新增职位 
				url = "/Hospital/permissionController/createPosition";
				text = "确认创建?";
			}
			if(operator == 2) {
				//修改职位 
				url = "/Hospital/permissionController/alterPosition";
				text = "确认修改?";
			}
			
			layer.confirm(text, {icon: 3, title:'提示'}, function(index){
				 $.ajax({
					type : "post",
					url : url,
					data : param,
					success : function(response) {
						//console.log("insertToDB response");
						//console.log(response);
						if(response.code != 200) {
							console.error(response);
							layer.msg("修改失败，具有自身不具备的权限",{icon:1,time:1000});
						}
						if(response.code == 200) {
							layer.msg("修改成功",{icon:1,time:1000});
						}
						var loginUser = JSON.parse(localStorage.getItem("loginUser"));
						var param = {
							clinicId : loginUser.clinicId,
						};
						layer.close(index);
						layer.close(mainFrame);
						table.reload('positionTable', {
							url : "/Hospital/permissionController/getAllPosition",
							method : "post",
							where : "",
							response : {
								statusName : "code",
								statusCode : "200",
								countName : "total",
								msgName : "msg",
								dataName : "result",
							}
						});
					}
				});
			});
		}
		//新增按钮 监听事件
		function btnAddEvent() {
			//positionTable_operator: 1(新增);2(编辑);
			$("#btn_add").on("click",function() {
				localStorage.setItem("positionTable_operator", 1);
				layer.open({
					type : 2,
					title : "新建职位",
					area : ["80%","80%"],
					content : "/Hospital/views/positionManager/positionDetail.html",
					btn : ["确认","取消"],
					btnAlign : "r",
					resize : true,
					yes : function(index,layero) {
						var param = $(layero).find("iframe")[0].contentWindow.getParam();
						var mainFrame = index;
						
						console.log("positionTable.detail.param");
						console.log(param);
						if(!vertify(param)) {
							return;
						}
						
						//插入数据库
						insertToDB(param,mainFrame);
					},
					cancel : function(index,layero) {
						layer.close(index);
					},
					btn2 : function(index,layero) {
						layer.close(index);
					}
				});
			});
		};
	});
	
	//生成表格列
	function createCols() {
		var permission = JSON.parse(localStorage.getItem("loginUser")).permission;
		//如果没有修改职位权限
		if(permission.alterPosition != 5) {
			var tableWidth=window.innerWidth*0.198;
			cols = [[
		                {field:'positionName',width:tableWidth,title:'职位',align:'center'},
		                {field:'creatorName',width:tableWidth,title:'创建者',align:'center'},
		                {field:'description',width:tableWidth,title:'描述',align:'center'},
		                {field:'rankInt',width:tableWidth,title:'权限等级',sort:true,align:'center'},
		                {field:'createTime',width:tableWidth,title:'创建时间',sort:true,align:'center'},
		           ]];
			return cols;
		}
		//如果有修改职位权限
		var tableWidth=window.innerWidth*0.186;
		cols = [[
	                {field:'positionName',width:tableWidth,title:'职位',align:'center'},
	                {field:'creatorName',width:tableWidth,title:'创建者',align:'center'},
	                {field:'description',width:tableWidth,title:'描述',align:'center'},
	                {field:'rankInt',width:tableWidth,title:'权限等级',sort:true,align:'center'},
	                {field:'createTime',width:tableWidth,title:'创建时间',sort:true,align:'center'},
	          		{toolbar:'#operator',width:70,title:'操作',fixed: 'right'},
	           ]];
		return cols;
	}
	
	//验证数据 false:数据不合法;true:数据合法
	function vertify(param) {
		if(null == param.name || param.name == "") {
			layer.msg("职位名称不能为空",{icon:2,time:1000});
			return false;
		}
		return true;
	}
	
	//TODO 权限设置问题
	//如果没有修改直接显示修改成功
	//param:修改后的值；data:原来的值
	//true :未修改；false:修改 需要插入数据库
	function isAlter(param,data) {
		//console.log("isAlter param");
		//console.log(param);
		//console.log("isAlter data");
		//console.log(data);
		
		//判断权限是否更新设置
		var paramPermission = JSON.parse(param.permissionIds);
		
		if(param.description == data.description && param.name == data.positionName 
				&& param.rank == data.rankInt 
				&& paramPermission.sort().toString() == data.permission.sort().toString()){
			layer.msg("修改成功",{icon:1,time:1000});
			return true;
		}
		return false;
	}
	
</script>
</html>
